package com.yunfei.api.v2.service;
import com.yunfei.api.v2.enums.GetAccountInfoEnum;
import com.yunfei.api.v2.param.AddAccountParam;
import com.yunfei.api.v2.param.UpdateAccountParam;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.AccountInfoMapper;
import com.yunfei.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @Description: 填写账户信息（投资者）
* @Author: zhengyunfei
* @Date: 2019/8/9
* @time:10:48
*/
@Service
public class HTSAddAccountService {
    @Autowired
    private AccountInfoMapper accountInfoMapper;
    /** 
    * @Description: 填写账户信息 
    * @Author: zhengyunfei
    * @Date: 2019/8/15  
    * @time:17:10        
    */ 
    public ResultModel add(AddAccountParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveNo=param.getcReserveNo();//预约编号
            String creatorId=param.getCreatorId();//合同生成用户
            String bankNo=param.getBankNo();//银行编号
            String subBankNm1=param.getSubBankNm1();//分行名称
            String subBankNm2=param.getSubBankNm2();//支行名称
            String acctNo=param.getAcctNo();//受益账号
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(GetAccountInfoEnum.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(creatorId)){
                resultModel.setCode(GetAccountInfoEnum.CREATOR_ID_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.CREATOR_ID_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(bankNo)){
                resultModel.setCode(GetAccountInfoEnum.BANK_NO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.BANK_NO_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(subBankNm1)){
                resultModel.setCode(GetAccountInfoEnum.SUBBANK_NM1_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.SUBBANK_NM1_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(subBankNm2)){
                resultModel.setCode(GetAccountInfoEnum.SUBBANK_NM2_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.SUBBANK_NM2_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(acctNo)){
                resultModel.setCode(GetAccountInfoEnum.ACCT_NO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.ACCT_NO_NOTNULL.getMessage());
                return resultModel;
            }
            //判断是否填写了账户信息
            Map<String,Object> queryAccountInfoMap=new HashMap<>();
            queryAccountInfoMap.put("reserveNo",reserveNo);
            int isWhiteAccountInfo=accountInfoMapper.isWhiteAccountInfo(queryAccountInfoMap);
            if(isWhiteAccountInfo==0){
                accountInfoMapper.add(param);
            }else{
                accountInfoMapper.update(param);
            }
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    /** 
    * @Description: 更新账户信息 
    * @Param: [param] 
    * @return: com.yunfei.bo.result.ResultModel 
    * @Author: zhengyunfei
    * @Date: 2019/8/15  
    * @time:17:31
    */ 
   public ResultModel update(UpdateAccountParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveNo=param.getcReserveNo();//预约编号
            String bankNo=param.getBankNo();//银行编号
            String subBankNm1=param.getSubBankNm1();//分行名称
            String subBankNm2=param.getSubBankNm2();//支行名称
            String acctNo=param.getAcctNo();//受益账号
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(GetAccountInfoEnum.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(bankNo)){
                resultModel.setCode(GetAccountInfoEnum.BANK_NO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.BANK_NO_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(subBankNm1)){
                resultModel.setCode(GetAccountInfoEnum.SUBBANK_NM1_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.SUBBANK_NM1_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(subBankNm2)){
                resultModel.setCode(GetAccountInfoEnum.SUBBANK_NM2_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.SUBBANK_NM2_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(acctNo)){
                resultModel.setCode(GetAccountInfoEnum.ACCT_NO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.ACCT_NO_NOTNULL.getMessage());
                return resultModel;
            }
            accountInfoMapper.update(param);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

}
